package com.dollarxy.dlmall.data.scheduled;

import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.dollarxy.dlmall.data.component.UmengWxMiniComponent;
import com.dollarxy.dlmall.data.dao.OverviewDao;
import com.dollarxy.dlmall.data.entity.*;
import com.dollarxy.dlmall.data.service.*;
import com.dollarxy.dlmall.data.util.DateUtils;
import com.umeng.umini.param.*;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class UmengScheduled5m {
    @Autowired
    UmengWxMiniComponent umengWxMiniComponent;


    @Autowired
    OverviewService overviewService;
    @Autowired
    OverviewDao overviewDao ;
    @Autowired
    EventOverViewService eventOverViewService;
    @Autowired
    SharePageOverviewService sharePageOverviewService;
    @Autowired
    CustomerSourceOverviewService customerSourceOverviewService;
    @Autowired
    TotalUserService totalUserService;

    String beginTime;
    String endTime;



    @Scheduled(cron = "* */5 * * * ? ")
    public void getUmengData() {


        ApiExecutor apiExecutor = new ApiExecutor("8537907", "UZfST6nSjl");
        apiExecutor.setServerHost("gateway.open.umeng.com");
        beginTime = DateUtils.format(new Date(),DateUtils.DATE_PATTERN);
        endTime = DateUtils.format(new Date(),DateUtils.DATE_PATTERN);

        try {
            //Overview data
            UmengWxMiniComponent umengWxMiniComponent = new UmengWxMiniComponent();
            UmengUminiGetOverviewResult result = umengWxMiniComponent.umengUminiGetOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");

            UmengUminiAppIndicatorDTO umengUminiAppIndicatorDTO = result.getData().getData()[0];
            OverviewEntity overviewEntity = new OverviewEntity();
            BeanUtils.copyProperties(umengUminiAppIndicatorDTO, overviewEntity);
            overviewEntity.setType(1);
            overviewService.save(overviewEntity);
            Thread.sleep(5000);

            //event data
            UmengUminiGetEventOverviewResult resultEventOverview_banner_click = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","banner_click");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO_banner_click = resultEventOverview_banner_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity = new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO_banner_click, eventOverViewEntity);
            eventOverViewEntity.setType(1);
            eventOverViewEntity.setDataType("banner_click");
            eventOverViewService.saveOrUpdate(eventOverViewEntity);
            Thread.sleep(5000);

            UmengUminiGetEventOverviewResult resultEventOverview_article_click = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","article_click");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO = resultEventOverview_article_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity_article_click = new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO, eventOverViewEntity_article_click);
            eventOverViewEntity_article_click.setType(1);
            eventOverViewEntity_article_click.setDataType("article_click");
            eventOverViewService.saveOrUpdate(eventOverViewEntity_article_click);
            Thread.sleep(5000);

            UmengUminiGetEventOverviewResult resultEventOverview_navigator_click = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","navigator_click");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO_navigator_click = resultEventOverview_article_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity_navigator_click = new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO, eventOverViewEntity_navigator_click);
            eventOverViewEntity_navigator_click.setType(1);
            eventOverViewEntity_navigator_click.setDataType("navigator_click");
            eventOverViewService.saveOrUpdate(eventOverViewEntity_navigator_click);
            Thread.sleep(5000);

            UmengUminiGetEventOverviewResult resultEventOverview_interactive_click = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","interactive_click");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO_interactive_click = resultEventOverview_article_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity_interactive_click = new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO, eventOverViewEntity_interactive_click);
            eventOverViewEntity_interactive_click.setType(1);
            eventOverViewEntity_interactive_click.setDataType("interactive_click");
            eventOverViewService.saveOrUpdate(eventOverViewEntity_interactive_click);
            Thread.sleep(5000);


            UmengUminiGetEventOverviewResult resultEventOverview_auth = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","auth");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO_auth = resultEventOverview_article_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity_auth = new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO, eventOverViewEntity_auth);
            eventOverViewEntity_auth.setType(1);
            eventOverViewEntity_auth.setDataType("auth");
            eventOverViewService.saveOrUpdate(eventOverViewEntity_auth);
            Thread.sleep(5000);

            UmengUminiGetEventOverviewResult resultEventOverview_search = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","search");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO_search= resultEventOverview_article_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity_search = new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO, eventOverViewEntity_search);
            eventOverViewEntity_search.setType(1);
            eventOverViewEntity_search.setDataType("search");
            eventOverViewService.saveOrUpdate(eventOverViewEntity_search);
            Thread.sleep(5000);


            UmengUminiGetEventOverviewResult resultEventOverview_article_view = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","article_view");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO_article_view = resultEventOverview_article_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity_article_view= new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO, eventOverViewEntity_article_view);
            eventOverViewEntity_article_view.setType(1);
            eventOverViewEntity_article_view.setDataType("article_view");
            eventOverViewService.saveOrUpdate(eventOverViewEntity_article_view);
            Thread.sleep(5000);

            UmengUminiGetEventOverviewResult resultEventOverview_page_view = umengWxMiniComponent.umengUminiGetEventOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","page_view");
            UmengUminiEventIndicatorDTO umengUminiEventIndicatorDTO_page_view = resultEventOverview_article_click.getData().getData()[0];
            EventOverViewEntity eventOverViewEntity_page_view= new EventOverViewEntity();
            BeanUtils.copyProperties(umengUminiEventIndicatorDTO, eventOverViewEntity_page_view);
            eventOverViewEntity_page_view.setType(1);
            eventOverViewEntity_page_view.setDataType("page_view");
            eventOverViewService.saveOrUpdate(eventOverViewEntity_page_view);
            Thread.sleep(5000);


            //share data
            UmengWxMiniComponent umengWxMiniComponent_share = new UmengWxMiniComponent();
            UmengUminiGetSharePageOverviewResult result_share = umengWxMiniComponent_share.umengUminiGetSharePageOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","share");

            UmengUminiSharePageIndicatorDTO umengUminiSharePageOverviewDTO = result_share.getData().getData()[0];
            SharePageOverviewEntity sharePageOverviewEntity = new SharePageOverviewEntity();
            BeanUtils.copyProperties(umengUminiSharePageOverviewDTO, sharePageOverviewEntity);
            sharePageOverviewEntity.setType(1);
            sharePageOverviewService.save(sharePageOverviewEntity);
            Thread.sleep(5000);

            UmengWxMiniComponent umengWxMiniComponent_custom = new UmengWxMiniComponent();
            UmengUminiGetCustomerSourceOverviewResult result_customer = umengWxMiniComponent_custom.umengUminiGetCustomerSourceOverview(apiExecutor,beginTime,beginTime,"5min","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration","share");
            List<CustomerSourceOverviewEntity> col = new ArrayList<CustomerSourceOverviewEntity>();
            for(UmengUminiCustomerSourceDTO v : result_customer.getData()){
                if(v.getLaunch()!=0 || v.getActiveUser() !=0 || v.getNewUser() !=0) {
                    CustomerSourceOverviewEntity customerSourceOverviewEntity = new CustomerSourceOverviewEntity();
                    BeanUtils.copyProperties(v, customerSourceOverviewEntity);
                    customerSourceOverviewEntity.setType(1);
                    col.add(customerSourceOverviewEntity);
                }
            }
            customerSourceOverviewService.saveBatch(col);
            Thread.sleep(5000);

            UmengWxMiniComponent umengWxMiniComponent_ov = new UmengWxMiniComponent();
            UmengUminiGetOverviewResult result_ov = umengWxMiniComponent_ov.umengUminiGetOverview(apiExecutor,beginTime,endTime,"30day","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");

            if(null !=result_ov && result_ov.getData() !=null && result_ov.getData().getData() !=null){
                UmengUminiAppIndicatorDTO umengUminiAppIndicatorDTO_ov = result_ov.getData().getData()[0];
                OverviewEntity overviewEntity_ov = new OverviewEntity();
                BeanUtils.copyProperties(umengUminiAppIndicatorDTO_ov, overviewEntity_ov);
                overviewEntity.setType(5);
                overviewService.save(overviewEntity_ov);
                Thread.sleep(5000);
            }


            UmengWxMiniComponent umengWxMiniComponent_dov = new UmengWxMiniComponent();
            UmengUminiGetOverviewResult result_dov = umengWxMiniComponent_dov.umengUminiGetOverview(apiExecutor,beginTime,endTime,"day","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");
            UmengUminiAppIndicatorDTO umengUminiAppIndicatorDTO_dov = result_dov.getData().getData()[0];
            OverviewEntity overviewEntity_dov = new OverviewEntity();
            BeanUtils.copyProperties(umengUminiAppIndicatorDTO_dov, overviewEntity_dov);
            overviewEntity.setType(3);
            overviewService.save(overviewEntity_dov);
            Thread.sleep(5000);

            UmengUminiGetTotalUserResult resultTotalUserResult_tu = umengWxMiniComponent.umengUminiGetTotalUser(apiExecutor,beginTime,endTime,"day","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");
            UmnegUminiTotalUserDTO umnegUminiTotalUserDTO_tu = resultTotalUserResult_tu.getData()[0];
            TotalUserEntity totalUserEntity_tu = new TotalUserEntity();
            BeanUtils.copyProperties(umnegUminiTotalUserDTO_tu, totalUserEntity_tu);
            totalUserEntity_tu.setType(1);
            totalUserService.save(totalUserEntity_tu);
            Thread.sleep(5000);

             //Overview data
            UmengWxMiniComponent umengWxMiniComponent_7o = new UmengWxMiniComponent();
            UmengUminiGetOverviewResult result_70 = umengWxMiniComponent_7o.umengUminiGetOverview(apiExecutor,beginTime,endTime,"7day","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");
            if(null !=result_ov && result_ov.getData() !=null && result_ov.getData().getData() !=null) {
                UmengUminiAppIndicatorDTO umengUminiAppIndicatorDTO_7o = result_70.getData().getData()[0];
                OverviewEntity overviewEntity_7o = new OverviewEntity();
                BeanUtils.copyProperties(umengUminiAppIndicatorDTO_7o, overviewEntity_7o);
                overviewEntity.setType(4);
                overviewService.save(overviewEntity);
                Thread.sleep(50000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            lock.unlock();
        }
    }
}
